# //
//  ProductDetailImageCell.m
//  FloatClouds
//
//  Created by 黄威 on 16/9/13.
//  Copyright © 2016年 黄威. All rights reserved.
//

#import "ProductDetailImageCell.h"
@interface ProductDetailImageCell()<UIScrollViewDelegate,UIGestureRecognizerDelegate>
@property (nonatomic, assign) CGFloat lastScale;

@end

@implementation ProductDetailImageCell

- (instancetype)initWithFrame:(CGRect)frame{
    
    self = [super initWithFrame:frame];
    
    if (self) {
        
        
        self.imageView = [[UIImageView alloc]init];
        
        self.imageView.frame = CGRectMake(0, self.bounds.size.height * 0.2, self.bounds.size.width, self.bounds.size.height * 0.4);
        
        self.imageView.userInteractionEnabled = YES;
        
        
        
        [self.contentView addSubview:self.imageView];
        
        UIPinchGestureRecognizer *pinch = [[UIPinchGestureRecognizer alloc]initWithTarget:self action:@selector(scale:)];
        
        UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(pan:)];
        pan.delegate = self;
        [self.imageView addGestureRecognizer:pinch];
        [self.imageView addGestureRecognizer:pan];
        
        
        
        
    }
    
    return self;
}

- (void)pan:(UIPanGestureRecognizer *)pan{
    
    if (self.imageView.transform.a != 1 ){
        
        
        CGPoint point = [pan translationInView:self.imageView];
        
        pan.view.center = CGPointMake(pan.view.center.x + point.x * 8, pan.view.center.y + point.y * 8 );
        [pan setTranslation:CGPointMake(0, 0) inView:self.imageView];}
    
}

- (void)scale:(UIPinchGestureRecognizer *)pinch{
    
    
    //当手指离开屏幕时,将lastscale设置为1.0
    if([pinch state] == UIGestureRecognizerStateEnded) {
        self.lastScale = 1.0;
        return;
    }
    
    CGFloat scale = 1.0 - (self.lastScale - [(UIPinchGestureRecognizer*)pinch scale]);
    
    CGAffineTransform currentTransform = self.imageView.transform;
    CGAffineTransform newTransform = CGAffineTransformScale(currentTransform, scale, scale);
    
    
    [self.imageView setTransform:newTransform];
    self.lastScale = [pinch scale];
    
}



- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    
    
}


-(BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldReceiveTouch:(UITouch *)touch{
    if (touch.view != self.imageView) {
        return NO;
    }
    
    return YES;
}

- (void)scrollViewDidZoom:(UIScrollView *)scrollView{
    
}
@end